import inspect
from typing import Any
from pydantic import confloat

from common.base.base_api import BaseApi
from services.ocb.schemas.ocb_registers import OcbStates, OcbThresholds, OcbCurrents28v
from common.utils import make_literal
from settings import settings


class OcbApi(BaseApi):
    __slots__ = []

    def __init__(self):
        super(OcbApi, self).__init__(request_queue_uuid=settings.OCB_REQUEST_QUEUE)

    async def set_ocb_threshold(self, name: make_literal(settings.OCB_METHODS_KEYS), value: confloat(ge=0, le=7)
                          ) -> bool | None:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {'name': name, 'value': value}
        )

    async def get_all_ocb_thresholds(self) -> OcbThresholds | None:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            handle_result=lambda result: OcbThresholds(**result)
        )

    async def get_ocb_threshold(self, name: make_literal(settings.OCB_METHODS_KEYS)) -> float | None:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'name': name})

    async def get_all_ocb_current_28v(self) -> OcbCurrents28v | None | Any:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            handle_result=lambda result: OcbCurrents28v(**result)
        )

    async def get_all_ocb_states(self) -> OcbStates | None | Any:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            handle_result=lambda result: OcbStates(**result)
        )

    async def read_services_ocb(self) -> list[bool]:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name)

    async def get_ocb_in_work_state(self) -> list[bool]:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name)
